package 数组;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Weton Li
 * @Date 2021/4/27 21:15
 */
public class 汇总区间 {

    public static List<String> method(int[] nums) {
        int n = nums.length;
        int i = 0;
        ArrayList<String> list = new ArrayList<>();
        while (i < n) {
            int low = i++;
            // 若在原数组上遍历，必须先++，再比较前一个数
            while (i < n && nums[i] == nums[i - 1] + 1) {
                i++;
            }
// 此行不能if
            int high = i - 1;
            StringBuilder sb = new StringBuilder(Integer.toString(nums[low]));
            if (low < high) {
                sb.append("->");
                sb.append(Integer.toString(nums[high]));
            }
            // 不管low<=>high，均添加到集合中
            list.add(sb.toString());

        }
        return list;
    }

    public static void main(String[] args) {
        int[] input = {0, 1, 2, 4, 5, 7};
        List<String> list = method(input);
//        for (String s : list) {
//            System.out.println(s);
//        }
        System.out.println(list);

    }
}
